package com.xueyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xueyu.dao.drugsDao;
import com.xueyu.domain.drugs.drugsBean;
import com.xueyu.util.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "drugs")
public class drugsController {
    @Autowired
    drugsDao drugsDao;
    @RequestMapping("/add")
    public ResultBean add(@RequestBody drugsBean drugsBean) throws Exception{
        ResultBean ResultBean= new ResultBean();
        if (drugsBean.getdrugID()==0){
            drugsDao.insert(drugsBean);
        }else {
            drugsDao.updateById(drugsBean);
        }
        return ResultBean;
    }
    @RequestMapping("/del/{drugID}")
    public ResultBean del(@PathVariable("drugID") int drugID){
        drugsDao.deleteById(drugID);
        return new ResultBean();
    }
    @RequestMapping("/getid/{drugID}")
    public Map<String, Object> getid(@PathVariable("drugID") int drugID){
        ResultBean resultBean = new ResultBean();
        if (drugID>0){
            QueryWrapper<drugsBean> queryWrapper = Wrappers.query();
            queryWrapper.eq("drugID",drugID);
            List<drugsBean> durgsList = drugsDao.selectList(queryWrapper);
            resultBean.put("data",durgsList);
        }
        return resultBean;
    }



    @RequestMapping("/fy")
    public ResultBean fy(@RequestBody Map map){
        int current = (Integer)map.get("Current");
        int size = (Integer)map.get("size");
        Page<drugsBean> page = new Page<>(current, size);
        QueryWrapper<drugsBean> queryWrapper = createSchoolQueryWrapper(map);
        page = drugsDao.selectPage(page,queryWrapper);
        List<drugsBean> list = page.getRecords();
        ResultBean resultBean = new ResultBean();
        resultBean.put("page",page);
        resultBean.put("list",list);
        return resultBean;
    }
    @RequestMapping("/zd")
    public ResultBean zd(@RequestBody Map map){
        QueryWrapper<drugsBean> queryWrapper =  createSchoolQueryWrapper(map);
        List<drugsBean> list = drugsDao.selectList(queryWrapper);
        ResultBean resultBean = new ResultBean();
        resultBean.put("list",list);
        return resultBean;
    }
    private QueryWrapper<drugsBean> createSchoolQueryWrapper(Map map) {
        String drugID = (String) map.get("drugID");
        String drugNames = (String) map.get("drugNames");
        QueryWrapper<drugsBean> queryWrapper = Wrappers.query();
        if (drugID != null && !"".equals(drugID)) {
            queryWrapper.eq("drugID", drugID);
        }
        if (drugNames != null && !"".equals(drugNames)) {
            queryWrapper.like("drugNames", "%" + drugNames + "%");
        }
        return queryWrapper;
    }
}
